<template>
  <div class="base-collapse" ref="collapseRefs" @scroll="$emit('scroll', $event)" :style="resizeStyle" @mousemove="handleResizeMove">
    <div v-show="showLeft" :class="['base-collapse-left', {'active': collapse == 'left'}]" :style="{flexBasis: `${realLeftWidth}px`}">
      
      <slot name="left"></slot>
      
      <!-- start 折叠按钮 -->
      <div class="base-collapse-btn" v-if="showCollapse">
        <el-tooltip :content="$t('common.base.collapse')" placement="left">
          <div
            class="base-collapse-btn-left"
            v-show="collapse != 'left'"
            @click="expand('left')"
          >
            <i class="iconfont icon-mianbanjiantou"></i>
          </div>
        </el-tooltip>
        <el-tooltip :content="$t('common.base.expand')" placement="right">
          <div
            class="base-collapse-btn-right"
            v-show="collapse != 'right'"
            @click="expand('right')"
          >
            <i class="iconfont icon-mianbanjiantou"></i>
          </div>
        </el-tooltip>
      </div>
      <!-- end 折叠按钮 -->
    </div>
    
    <!-- 是否支持左右拖拽 -->
    <div  
      v-show="showResize" 
      class="resize-bar" 
      :class="resizeBarClassNames"
      @mouseover="showRise=true" 
      @mouseleave="showRise=false"
    >
      <div
        v-show="resize && showRise && !direction"
        class="resize-bar-content"
        @mousedown="handleResizeBar"
      >
        <slot name="resize"><div>⋮</div></slot>
      </div>
    </div>

    <div v-show="showRight" :class="['base-collapse-right', {'active': collapse == 'right'}]" 
    :style="{flexBasis: `${realRightWidth}px`, marginLeft: resize ? 0 : '12px'}">
      <slot name="right"></slot>
      <div
        @click="expand('right')"
        v-if="collapse == 'right'"
        class="rightExtend">
        <slot name="rightExtend"></slot>
      </div>
    </div>
    
  </div>
</template>

<script>
export default {
  name: 'base-collapse',
  props: {
    direction: {
      type: String,
      default: '',
    },
    showCollapse: {
      type: Boolean,
      default: true,
    },
    leftSize: {
      type: Number,
      default: 1,
    },
    rightSize: {
      type: Number,
      default: 1,
    },
    resize: {
      type: Boolean,
      default: true
    },
    minScrollWidth: {
      type: Number,
      default: 320
    },
    iframeId: {
      type: String,
      default: '',
    },
    // 通栏布局隐藏左右折叠 left：隐藏左栏 right：隐藏右栏
    hidePartCollapse: {
      type: String,
      default: '',
    }
  },
  data() {
    return {
      collapse: this.direction,
      realLeftWidth: 0, // 左侧 width
      realRightWidth: 0, // 右侧 width
      containerW: 0,
      resizeHandle: false,
      origin: {},
      showRise: false,
    };
  },
  computed: {
    resizeStyle() {
      if (this.resizeHandle) {
        return {
          userSelect: 'none',
        };
      }
      return null;
    },
    resizeBarClassNames() {
      return {
        'resize-bar-active': this.showRise && this.showCollapse && !this.direction
      }
    },
    showLeft() {
      return this.hidePartCollapse != 'left';
    },
    showRight() {
      return this.hidePartCollapse != 'right';
    },
    showResize() {
      return !this.hidePartCollapse
    },
  },
  mounted() {
    this.calcWidth();
    window.addEventListener( 'resize', ()=> this.$nextTick(()=> this.calcWidth()));
  },
  beforeDestroy() {
    window.removeEventListener( 'resize', ()=> {});
  },
  methods: {
    /**
     * @description 折叠
     */
    expand(direction) {
      // 折叠过，则恢复
      if (this.collapse) {
        this.calcWidth();
        return this.collapse = ''
      }

      this.collapse = direction;
    },
    handleResizeMove(e) {
      if(!this.resizeHandle) return;
      const { x, width, min, max } = this.origin;
      let distance = e.clientX - x;
      let w = width + distance;
      if (w < min) {
        w = min;
      } else if (w > max) {
        w = max;
      }
      this.calcWidth(w);
    },
    handleMouseUp() {
      this.resizeHandle = false;
      document.removeEventListener('mouseup', this.handleMouseUp);
      if(this.iframeId) {
        const currentIframe = document.getElementById(this.iframeId)
        currentIframe?.style && (currentIframe.style.pointerEvents="auto");
      }
    },
    handleResizeBar(e) {
     if(this.iframeId) {
       const currentIframe = document.getElementById(this.iframeId)
       currentIframe?.style && (currentIframe.style.pointerEvents="none");
     }
      this.resizeHandle = true;
      document.addEventListener('mouseup', this.handleMouseUp);
      const min = this.minScrollWidth || 110;
      this.containerW = this.$refs.collapseRefs.clientWidth;
      this.origin = {
        x: e.clientX, // 相对于浏览器可视区域的x轴坐标
        width: this.realLeftWidth,
        min: min,
        max: this.$refs.collapseRefs.clientWidth - min,
      };
    },
    calcWidth(leftW, rightW) {
      const { resize, containerW } = this;
      const resizeW = resize ? 5 : 0;
      if (leftW) {
        this.realLeftWidth = leftW;
        this.realRightWidth = containerW - resizeW - leftW;
      } else if (rightW) {
        this.realLeftWidth = containerW - resizeW - rightW;
        this.realRightWidth = rightW;
      } else {
        const containerW = this.$refs?.collapseRefs?.clientWidth;
        const totalSize = this.leftSize + this.rightSize;

        // 左右各占一半
        this.realLeftWidth = containerW * (this.leftSize / totalSize);
        this.realRightWidth = containerW * (this.rightSize / totalSize);
      }
    },
  },
  watch: {
    collapse(newValue) {
      this.$emit('update:direction', newValue);
    },
  },
};
</script>

<style lang="scss">
.base-collapse {
  position: relative;
  overflow: hidden;
  width: 100%;
  display: flex;
  flex-flow: row nowrap;
  
  &-left,
  &-right {
    position: relative;
    max-width: 100%;
    min-width: 128px;
    height: 100%;
    
    background-color: #fff;
    border-radius: 4px;
    
    transition-duration: 300ms;
    
    &.active {
      max-width: 128px;
      flex: 1!important;
    }
  }

  &-left,
  &-right {
    flex-grow: 1;
    flex-shrink: 1;
    flex-basis: auto;
  }
  
  &-btn {
    width: 10px;
    position: absolute;
    right: -10px;
    top: 50%;
    transform: translateY(-50%);
    z-index: 9;
    
    &-left,
    &-right {
      width: 11px;
      height: 34px;
      
      background-color: #fff;
      border: 1px solid $color-border-l2;
      border-left: none;
      border-top-right-radius: 4px;
      border-bottom-right-radius: 4px;
      
      display: flex;
      align-items: center;
      justify-content: center;
      
      cursor: pointer;
      
      .iconfont {
        font-size: 12px;
        color: $text-color-secondary;
      }
      
      &:hover {
        background-color: $color-primary-light-1;
        border-color: $color-primary-light-2;

        
        .iconfont {
          @include fontColor();
        }
      }
    }
    
    &-left {
      margin-bottom: 8px;
      
      .iconfont {
        transform: rotate(-180deg) translateX(1px);
      }
    }
  }

  .resize-bar {
    width: 5px;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    box-sizing: border-box;
    margin: 0 4px;
    &-content {
      width: 5px;
      height: 100%;
      background: #e5e5e5;
      color: white;
      display: flex;
      justify-content: center;
    }
  }
}
.resize-bar-active {
  cursor: move;
}
</style>
